package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.db.Option;
import com.itheima.mapper.TestSoulMapper;
import com.itheima.service.db.OptionSoulService;
import com.itheima.vo.OptionsVo;
import com.itheima.vo.QuestionsVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Service
@RequiredArgsConstructor
public class OptionSoulServiceImpl extends ServiceImpl<TestSoulMapper, Option> implements OptionSoulService {


    @Override
    public List<OptionsVo> findOptions() {
        // 查询数据库所有数据
        List<Option> list = lambdaQuery().list();
        //把List<Option>转化为List<OptionsVo>
        List<OptionsVo> OptionsVos = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (Option option : list) {
                OptionsVo optionsVo = new OptionsVo(option.getId(), option.getContent());
                OptionsVos.add(optionsVo);
            }
        }
        return OptionsVos;
    }

    @Override
    public List<OptionsVo> findOptionsByQuestionId(String id1) {
        //根据问题id查询对应选项option
        List<Option> list = lambdaQuery().eq(Option::getQuestionId, id1).list();
        List<OptionsVo> OptionsVos = new ArrayList<>();

        //把List<Option>转化为List<OptionsVo>
        if (list != null && !list.isEmpty()) {
            for (Option option : list) {
                OptionsVo optionsVo = new OptionsVo(option.getId(), option.getContent());
                OptionsVos.add(optionsVo);
            }
            return OptionsVos;
       }
        return Collections.emptyList();
    }

    @Override
    public Integer findScoreById(String questionId, String optionId) {
        Option option = lambdaQuery().eq(Option::getId, optionId).eq(Option::getQuestionId, questionId).one();
        return Integer.valueOf(option.getScore());
    }

}
